// T_Study1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "gl/glew.h"
#include "gl/glut.h"
#include "il/il.h"
#include "il/ilu.h"
#include "il/ilut.h"

class ATexture
{
	friend class ATextureFilter;
public:
	bool read(const char* imgName);
	void destroy();
	bool saveAs(const char* imgName);
	bool saveAs2(const char* imgName);
	bool create(const char* imgName);
	ILuint handle;
	ILuint handle2;
	bool bLoadSuc;
	int width;
	int height;
	unsigned char *imgBuf;
	unsigned char *imgBuf2;
};

bool ATexture::read(const char* imgName)
{
	ilGenImages(1,&handle);
	ilBindImage(handle);
	bLoadSuc = (IL_TRUE == ilLoadImage(imgName));
	if(bLoadSuc){
		height = ilGetInteger(IL_IMAGE_HEIGHT);
		width = ilGetInteger(IL_IMAGE_WIDTH);
		ILenum type = ilGetInteger(IL_IMAGE_TYPE);
		printf("type = %x",type);
		ILboolean bConvSuc = (IL_TRUE==ilConvertImage(IL_RGBA,type));
		ILint s = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
		int memSize = height*width*4*sizeof(unsigned char);
		imgBuf = ilGetData();
		return true;
	}
	return false;
}

bool ATexture::create(const char* imgName)
{
	ilGenImages(1,&handle2);
	ilBindImage(handle2);
	bLoadSuc = (IL_TRUE == ilLoadImage(imgName));
	if(bLoadSuc){
		height = ilGetInteger(IL_IMAGE_HEIGHT);
		width = ilGetInteger(IL_IMAGE_WIDTH);
		ILenum type = ilGetInteger(IL_IMAGE_TYPE);
		printf("type = %x",type);
		ILboolean bConvSuc = (IL_TRUE==ilConvertImage(IL_RGBA,type));
		ILint s = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
		int memSize = height*width*4*sizeof(unsigned char);
		imgBuf2 = ilGetData();
		return true;
	}
	return false;
}

void ATexture::destroy()
{
	if(bLoadSuc){
		ilDeleteImages(1,&handle);
	}
	bLoadSuc = false;
}

bool ATexture::saveAs(const char* imgName)
{
	ilBindImage(handle);
	return ilSaveImage(imgName);
}

bool ATexture::saveAs2(const char* imgName)
{
	ilBindImage(handle2);
	return ilSaveImage(imgName);
}
///////////////////////////////////////////////////////
class ATextureFilter
{
public:
	void change();
	ATexture* texture;
};

void ATextureFilter::change()
{
	int height = texture->height;
	int width = texture->width;
	for(int y = 0;y<height;++y){
		for(int x = 0;x<width;++x){
			int yy = (height-y-1);
			yy = y;
			texture->imgBuf2[y*width*4+x*4+0] = texture->imgBuf[yy*width*4+x*4+2];
			texture->imgBuf2[y*width*4+x*4+1] = texture->imgBuf[yy*width*4+x*4+1];
			texture->imgBuf2[y*width*4+x*4+2] = texture->imgBuf[yy*width*4+x*4+0];
			texture->imgBuf2[y*width*4+x*4+3] = texture->imgBuf[yy*width*4+x*4+3];
		}
	}
	ilBindImage(texture->handle2);
	ilSetData(texture->imgBuf2);
	//ilSetPixels(0,0,0,width,height,0,IL_RGBA,IL_UNSIGNED_BYTE,texture->imgBuf2);
};

void init(void)
{
	ilInit();
	ilEnable(IL_ORIGIN_SET);
}

int _tmain(int argc, char* argv[])
{
	init();
	ATexture texture;
	texture.read("chb.jpg");
	texture.create("chb.jpg");
	ATextureFilter filter;
	filter.texture = &texture;
	filter.change();
	bool bSuc = texture.saveAs2("chb2.jpg");
	texture.destroy();
	return 0;
}